package com.ptsolusi.ceriacrudwicket.tool.pages.windows;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;

import com.ptsolusi.ceriacrudwicket.tool.components.FocusOnLoadBehaviour;
import com.ptsolusi.ceriacrudwicket.tool.constant.OperationType;
import com.ptsolusi.ceriacrudwicket.tool.pages.CustomerList;

public class SearchCustomerPanel extends Panel {
	
	private static final long serialVersionUID = 1L;
	public static final String pFirstName = "searchString";

	public SearchCustomerPanel(String id, PageParameters params) {
		super(id);
		String param = null;
		if(params!=null)
			param = params.getString(pFirstName);
		add(new SearchForm("searchForm", param));
	}
	
	
	private class SearchForm extends Form {
		
		private static final long serialVersionUID = 1L;
		private String searchString;

		public SearchForm(String id, String param) {
			super(id);
			setSearchString(param);
			add(new TextField("searchString", new PropertyModel(this, "searchString")).add(new FocusOnLoadBehaviour()));
		}
		
		@Override
		protected void onSubmit() {
			super.onSubmit();
			System.out.println("SearchCustomerPanel - onSubmit");
			PageParameters params = new PageParameters();
			params.add(pFirstName, getSearchString());
			params.add(OperationType.SEARCH.toString(), OperationType.SEARCH.toString());
			setResponsePage(CustomerList.class, params);
		}

		public void setSearchString(String searchString) {
			this.searchString = searchString;
		}

		public String getSearchString() {
			System.out.println("getSearchString : "+searchString);
			return searchString;
		}
		
	}

}
